﻿using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Fast.Extensions.DatabaseAccessor.EntityFrameworkCore
{
    /// <summary>
    /// 更新仓储实现
    /// </summary>
    public partial class RootRepository<TEntity>
        where TEntity : class, IRootEntity, new()
    {
        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Update(TEntity entity)
        {
            Entities.Update(entity);
        }
        /// <summary>
        /// 更新一条记录并返回带跟踪信息的实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>代理中的实体</returns>
        public virtual EntityEntry<TEntity> UpdateWithReturnEntity(TEntity entity)
        {
            return Entities.Update(entity);
        }
        /// <summary>
        /// 更新多条记录
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual void Update(IEnumerable<TEntity> entities)
        {
            Entities.UpdateRange(entities);
        }
        /// <summary>
        /// 更新一条记录并立即提交
        /// </summary>
        /// <param name="entity"></param>
        public virtual void UpdateAndCommit(TEntity entity)
        {
            this.Update(entity);

            this.SaveChanges();
        }
        /// <summary>
        /// 更新一条记录并立即提交，并返回带跟踪信息的实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>代理中的实体</returns>
        public virtual EntityEntry<TEntity> UpdateAndCommitWithReturnEntity(TEntity entity)
        {
            var entityEntry = this.UpdateWithReturnEntity(entity);

            this.SaveChanges();

            return entityEntry;
        }
        /// <summary>
        /// 更新多条记录并立即提交
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual void UpdateAndCommit(IEnumerable<TEntity> entities)
        {
            this.Update(entities);

            this.SaveChanges();
        }
        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity"></param>
        public virtual async Task UpdateAsync(TEntity entity)
        {
            await Task.Run(() => this.Update(entity));
        }
        /// <summary>
        /// 更新一条记录并返回带跟踪信息的实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>代理中的实体</returns>
        public virtual async Task<EntityEntry<TEntity>> UpdateWithReturnEntityAsync(TEntity entity)
        {
            return await Task.Run(() => this.UpdateWithReturnEntity(entity));
        }
        /// <summary>
        /// 更新多条记录
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual async Task UpdateAsync(IEnumerable<TEntity> entities)
        {
            await Task.Run(() => this.Update(entities));
        }
        /// <summary>
        /// 更新一条记录并立即提交
        /// </summary>
        /// <param name="entity"></param>
        public virtual async Task UpdateAndCommitAsync(TEntity entity, CancellationToken cancellationToken = default)
        {
            await this.UpdateAsync(entity);

            await this.SaveChangesAsync(cancellationToken);
        }
        /// <summary>
        /// 更新一条记录并立即提交，并返回带跟踪信息的实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>代理中的实体</returns>
        public virtual async Task<EntityEntry<TEntity>> UpdateAndCommitWithReturnEntityAsync(TEntity entity, CancellationToken cancellationToken = default)
        {
            var entityEntry = await this.UpdateWithReturnEntityAsync(entity);

            await this.SaveChangesAsync(cancellationToken);

            return entityEntry;
        }
        /// <summary>
        /// 更新多条记录并立即提交
        /// </summary>
        /// <param name="entities">多个实体</param>
        public virtual async Task UpdateAndCommitAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default)
        {
            await this.UpdateAsync(entities);

            await this.SaveChangesAsync(cancellationToken);
        }
    }
}
